﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using Microsoft.Practices.Unity;
using Tencent.OA.ACM.Service.Interface;
using Tencent.OABanche.App;
using Tencent.OA.ACM.Entity;
using Tencent.OA.App;
using Tencent.OA.ACM.Proxy;
using System.Web.Script.Serialization;

namespace Tencent.OA.ACM.App.Controllers
{
    [HandleError]
    public class HomeController : BaseController
    {
        #region
        private TeamInfoProxy teamProxy = new TeamInfoProxy();
        private AppInfoProxy appProxy = new AppInfoProxy();
        #endregion

        public ActionResult Index()
        {
           // LoggerProvider.CurrentLogger.Info("Edd");
            return View();
        }
        /// <summary>
        /// 服务器视图的组显示
        /// </summary>
        /// <returns></returns>
        public ActionResult ServerTeam()
        {
            return new JsonResult();
        }

        /// <summary>
        /// 应用视图的组显示
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public ActionResult ApplicationTeam()
        {
            IList<TeamInfo> teamList = teamProxy.GetModelList(string.Empty);
            IList<AppInfo> appInfoList = appProxy.GetModelList(string.Empty);

            IList<TreeNode> trees = new List<TreeNode>();
            int orderId = 1;
            foreach (TeamInfo team in teamList)
            {
                IList<AppInfo> appList= appInfoList.Where(o => o.Team == team.ID).ToList();
                if (appList != null && appList.Count != 0)
                {
                    int parentId=orderId++;
                    TreeNode parentNode = new TreeNode() { ID = team.ID.ToString(), OrderID = parentId, ParentID = 0, ExtendText = string.Empty, Name = team.TeamName + "(" + appList.Count() + ")", Nodes = new List<TreeNode>() };
                    foreach (AppInfo app in appList)
                    {
                        parentNode.Nodes.Add(new TreeNode() { ID = app.ID.ToString(), OrderID = orderId++, ParentID = parentId, ExtendText = string.Empty, Name = app.AppName, Nodes = null });
                    }
                    trees.Add(parentNode);
                }
            }
            JavaScriptSerializer serializer = new JavaScriptSerializer();
            string jsString=serializer.Serialize(trees);
            JavaScriptResult jsResult=new JavaScriptResult();
            jsResult.Script=string.Format("var TreeObj={0};", jsString);
            return jsResult;
        }

    }
}
